package com.ylfstu.houserent.view;

import com.ylfstu.houserent.domain.House;
import com.ylfstu.houserent.service.HouseService;
import com.ylfstu.houserent.utils.Utility;

/**
 * 1.显示界面
 * 2.接收用户输入
 * 3.调用HouseService完成对房屋信息的各种操作
 */
public class HouseView {
    private boolean loop = true;
    private char key = ' ';
    private HouseService houseService = new HouseService(2);//假设数组大小为10

    //修改方法，根据房屋id修改房屋信息
    public void updateHouse() {
        System.out.println("==================修改房屋信息================");
        System.out.print("请输入要修改的房屋id(-1退出):");
        int updateId = Utility.readInt();
        if (updateId == -1) {
            System.out.println("================放弃删除房屋信息==============\n");
            return;
        }
        House house = houseService.findById(updateId);
        if (house == null) {
            System.out.println("=============房屋编号不存在，修改失败===========\n");
            return;
        }
        System.out.print("姓名(" + house.getName() + "):");
        String name = Utility.readString(8,"");//这里用户如果直接回车表示不修改该信息，默认""
        if (!"".equals(name)) {
            house.setName(name);
        }
        System.out.print("电话(" + house.getPhone() + "):");
        String phone = Utility.readString(12, "");
        if (!"".equals(phone)) {
            house.setPhone(phone);
        }
        System.out.print("地址(" + house.getAddress() + "):");
        String address = Utility.readString(16, "");
        if (!"".equals(address)) {
            house.setAddress(address);
        }
        System.out.print("月租(" + house.getRent() + "):");
        int rent = Utility.readInt(0);
        if (rent != 0) {
            house.setRent(rent);
        }
        System.out.println("状态(" + house.getState() + "):");
        String state = Utility.readString(3, "");
        if (!"".equals(state)) {
            house.setState(state);
        }
        System.out.println("=================修改房屋信息成功==============\n");

    }

    //查找方法  根据房屋id查找房屋
    public void findHouse() {
        System.out.println("==================查找房屋信息================");
        System.out.print("请输入要查找的房屋id:");
        int findId = Utility.readInt();
        //调用findById方法
        House house = houseService.findById(findId);
        if (house != null) {
            System.out.println(house);
        } else {
            System.out.println("=============房屋编号不存在，查找失败===========\n");
        }
    }

    //退出方法
    public void exit() {
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            loop = false;
        }
    }

    //编写delHouse(),接收输入的id，调用Service 的del方法
    public void delHouse() {
        System.out.println("==================删除房屋信息================");
        System.out.print("请输入待删除房屋的编号(-1退出):");
        int delId = Utility.readInt();
        if (delId == -1) {
            System.out.println("================放弃删除房屋信息==============\n");
            return;
        }
        char choice = Utility.readConfirmSelection();
        if (choice == 'Y') {
            if (houseService.del(delId)) {
                System.out.println("================删除房屋信息成功==============\n");
            } else {
                System.out.println("=============房屋编号不存在，删除失败===========\n");
            }
        } else {
            System.out.println("================放弃删除房屋信息==============\n");
        }
    }

    //编写addHouse(),接收输入，创建house对象，调用add
    public void addHouse() {
        System.out.println("==================添 加 房 屋================");
        System.out.print("姓名:");
        String name = Utility.readString(10);
        System.out.print("电话:");
        String phone = Utility.readString(12);
        System.out.print("地址:");
        String address = Utility.readString(16);
        System.out.print("月租:");
        int rent = Utility.readInt();
        System.out.print("状态:");
        String state = Utility.readString(3);
        //创建一个新对象，注意id是系统分配的，下面的0后面进入service会被改掉
        House newHouse = new House(0, name, phone, address, rent, state);
        if (houseService.add(newHouse)) {
            System.out.println("==================添加房屋成功================\n");
        } else {
            System.out.println("==================添加房屋失败================\n");
        }
    }

    //显示房屋列表
    public void listHouses() {
        System.out.println("==================房 屋 列 表================");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        House[] houses = houseService.list();
        for (int i = 0; i < houses.length; i++) {
            if (houses[i] == null) {
                break;
            }
            System.out.println(houses[i]);
        }
        System.out.println("=================房屋列表显示完毕===============\n");
    }

    //显示主菜单
    public void mainMenu() {

        do {
            System.out.println("==================房屋出租系统================");
            System.out.println("\t\t\t\t1 新 增 房 源");
            System.out.println("\t\t\t\t2 查 找 房 屋");
            System.out.println("\t\t\t\t3 删 除 房 屋 信 息");
            System.out.println("\t\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t\t5 显 示 房 屋 信 息");
            System.out.println("\t\t\t\t6 退      出");
            System.out.print("请输入你的选择(1~6):");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    addHouse();
                    break;
                case '2':
                    findHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouses();
                    break;
                case '6':
                    exit();
                    break;
            }

        } while (loop);
    }
}
